package cn.wei.reallyEnjoy.home.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import cn.wei.reallyEnjoy.R;
import cn.wei.reallyEnjoy.app.RecordDetailActivity;
import cn.wei.reallyEnjoy.home.adapter.SearchDiaryAdapter;
import cn.wei.reallyEnjoy.home.bean.DiaryBean;
import cn.wei.reallyEnjoy.home.bean.DiaryListBean;
import cn.wei.reallyEnjoy.home.bean.RecordBean;
import cn.wei.reallyEnjoy.user.activity.MyCollectionActivity;
import cn.wei.reallyEnjoy.user.adapter.MyCollectionAdapter;
import cn.wei.reallyEnjoy.utils.Constants;
import okhttp3.Call;

public class SearchDiaryActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText tvSearchInput;
    private ImageButton ib_search_back;
    private Button tvSearchBtn;
    private GridView gvSearchDiary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_diary);
        //去掉窗口标题
        getSupportActionBar().hide();
        findViews();
        //直接通过Intent接收
        String keyWord= getIntent().getStringExtra("keyWord");
        Log.e("Search",keyWord);
        tvSearchInput.setText(keyWord);

        initData(keyWord);
    }
    /**
     * Find the Views in the layout<br />
     * <br />
     * Auto-created on 2021-12-06 16:48:37 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    private void findViews() {

        tvSearchInput = (EditText)findViewById( R.id.tv_search_input );
        ib_search_back = (ImageButton)findViewById( R.id.ib_search_back );
        tvSearchBtn = (Button)findViewById( R.id.tv_search_btn );
        gvSearchDiary = (GridView)findViewById( R.id.gv_search_diary );
        tvSearchBtn.setOnClickListener( this );
        ib_search_back.setOnClickListener( this );
    }

    private void initData(String keyWord) {
        String url = Constants.SEARCH_DIARY_URL+keyWord;
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(SearchDiaryActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JSONObject jsonObject = JSON.parseObject(response);
                        String code = String.valueOf(jsonObject.get("code"));
                        if (TextUtils.equals(code,"0")){

                            String data = String.valueOf(jsonObject.get("data"));
                            DiaryListBean diaryListBean = JSON.parseObject(data, DiaryListBean.class);
                            if (diaryListBean.getUserDiary().size()>0){
                                gvSearchDiary.setAdapter(new SearchDiaryAdapter(diaryListBean.getUserDiary(), SearchDiaryActivity.this));
                                gvSearchDiary.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                        //日志记录对象
                                        DiaryBean diaryBean = diaryListBean.getUserDiary().get(position);
                                        RecordBean recordBean = new RecordBean();

                                        //记录对象
                                        recordBean.setRecord_id(diaryBean.getId());
                                        recordBean.setImg(diaryBean.getContentImg());
                                        recordBean.setContent(diaryBean.getContent());
                                        recordBean.setTitle(diaryBean.getTitle());
                                        recordBean.setUname(diaryBean.getUname());
                                        recordBean.setUheadImg(diaryBean.getUheadImg());
                                        recordBean.setPrefer(diaryBean.getPrefer());
                                        recordBean.setCreateTime(diaryBean.getCreateTime());
                                        startRecordDetailActivity(recordBean);
                                    }
                                });

                            }else{
                                Toast.makeText(SearchDiaryActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                                gvSearchDiary.setAdapter(null);
                            }

                        }else{
                            Toast.makeText(SearchDiaryActivity.this, "服务器错误", Toast.LENGTH_SHORT).show();

                        }
                    }
                });
    }
    private void startRecordDetailActivity(RecordBean recordBean) {
        Intent intent = new Intent(SearchDiaryActivity.this, RecordDetailActivity.class);
        intent.putExtra("recordBean",recordBean);
        startActivity(intent);
    }



    /**
     * Handle button click events<br />
     * <br />
     * Auto-created on 2021-12-06 16:48:37 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    @Override
    public void onClick(View v) {
        if ( v == tvSearchBtn ) {
            // Handle clicks for tvSearchBtn
            String keyWord = tvSearchInput.getText().toString().trim();
            if (TextUtils.equals(keyWord,"")){
                Toast.makeText(SearchDiaryActivity.this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
            }else{
                initData(keyWord);
            }
        }
        else if (v==ib_search_back){
            finish();
        }
    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}